package com.kanshu.ksgb.fastread.doudou.common.util;

import android.app.Activity;
import android.os.Parcelable;
import android.text.TextUtils;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 路由跳转封装工具类
 * 作者    wcy
 * 时间    2018/12/3 11:52
 * 文件    doudou
 * 描述
 */
public class ARouterUtils {
    /**
     * 跳转到页面
     *
     * @param url
     * @param params
     */
    public static void toActivity(String url, Map<String, ?> params) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        Postcard postcard = generatePostcard(url, params);
        postcard.navigation();
    }

    private static Postcard generatePostcard(String url, Map<String, ?> params) {
        Postcard postcard = ARouter.getInstance()
                .build(url);
        if (params != null) {
            for (Map.Entry<String, ?> entry : params.entrySet()) {
                String key = entry.getKey();
                Object value = params.get(key);
                if (value instanceof String) {
                    postcard.withString(key, (String) value);
                } else if (value instanceof Boolean) {
                    postcard.withBoolean(key, (boolean) value);
                } else if (value instanceof Integer) {
                    postcard.withInt(key, (int) value);
                } else if (value instanceof Float) {
                    postcard.withFloat(key, (float) value);
                } else if (value instanceof Double) {
                    postcard.withDouble(key, (double) value);
                } else if (value instanceof Long) {
                    postcard.withLong(key, (long) value);
                } else if (value instanceof Short) {
                    postcard.withShort(key, (short) value);
                } else if (value instanceof ArrayList) {
                    postcard.withSerializable(key, (ArrayList) value);
                } else if (value instanceof HashMap) {
                    postcard.withSerializable(key, (HashMap) value);
                } else if (value instanceof Parcelable) {
                    postcard.withParcelable(key, (Parcelable) value);
                }
            }
        }
        return postcard;
    }

    /**
     * 跳转到页面
     *
     * @param url
     * @param key
     * @param val
     */
    public static void toActivity(String url, String key, Object val) {
        Map<String, Object> map = new HashMap<>(1);
        map.put(key, val);
        toActivity(url, map);
    }

    /**
     * 不带参数的跳转
     *
     * @param url
     */
    public static void toActivity(String url) {
        toActivity(url, null);
    }

    public static void toActivityForResult(Activity activity, String url, Map<String, ?> params, int requestCode) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        Postcard postcard = generatePostcard(url, params);
        postcard.navigation(activity, requestCode);
    }

    public static void toActivityForResult(Activity activity, String url, int requestCode) {
        toActivityForResult(activity,url, null, requestCode);
    }
}
